﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SMS.dao;
using System.Windows.Forms;
using SMS.util;
using System.Data;
using System.Collections;

namespace SMS.cntr
{
    public class StudentCntr
    {
        private static readonly StudentCntr INSTANCE = new StudentCntr();

        public static StudentCntr Instance
        {
            get
            {
                return INSTANCE;
            }
        }

        private StudentCntr() { }

        public bool createStudent(String fullName, DateTime dob, String email, bool gender, String address)
        {
            if(validateCreatingStudent(fullName, dob, email, gender, address))
            {
                int result = StudentDao.Instance.insert(fullName, dob, email, gender, address);
                return result == 1 ? true : false;
            }

            return false;
        }

        private bool validateCreatingStudent(String fullName, DateTime dob, String email, bool gender, String address)
        {
            if (fullName.Equals(""))
            {
                MessageBox.Show("Cần nhập họ và tên");
                return false;
            }
            else if (!(PreferenceCntr.Instance.getMinAge() <= DateUtil.getAge(dob) &&
                     DateUtil.getAge(dob) <= PreferenceCntr.Instance.getMaxAge()))
            {
                MessageBox.Show("Tuổi phải lớn hơn 15 và nhỏ hơn 20");
                return false;
            }
            else if (address.Equals(""))
            {
                MessageBox.Show("Cần nhập địa chỉ");
                return false;
            }

            return true;
        }

        public DataTable findStudentsNotIn(int clazzId)
        {
            return StudentDao.Instance.findStudentsNotIn(clazzId);
        }

        public DataTable findStudentsIn(int clazzId)
        {
            return StudentDao.Instance.findStudentsIn(clazzId);
        }

        public DataTable findAll()
        {
            return StudentDao.Instance.findAll();
        }

        public ArrayList findById(int stuId)
        {
            return StudentDao.Instance.findById(stuId);
        }

		public DataTable findStudents(String clazzId)
        {
            return StudentDao.Instance.findStudents(clazzId);
        }

        public bool updateStudent(int stuId, String fullName, 
                                 DateTime dob, String email, bool gender, String address)
        {
            if (validateCreatingStudent(fullName, dob, email, gender, address))
            {
                int result = StudentDao.Instance.update(stuId, fullName, dob, email, gender, address);
                return result == 1 ? true : false;
            }
            return false;
        }

        public DataTable findBy(String fullName)
        {
            return StudentDao.Instance.findBy(fullName);
        }

        public DataTable findBy(int classId, int courseId, int semesterId)
        {
            return StudentDao.Instance.findBy(classId, courseId, semesterId);
        }


    }
}
